package com.icexxx.litchi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 端口工具类
 * 
 */
public class LitchiPortUtil {

    public static void close(int port) {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec("cmd /c netstat -ano | findstr \"" + port + "\"");
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            InputStream inputStream = process.getInputStream();
            List<String> content = read(inputStream, "UTF-8", port);
            if (content.size() == 0) {
                return;
            } else {
                killWithPid(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void killWithPid(List<String> pids) {
        for (String pid : pids) {
            try {
                Runtime.getRuntime().exec("taskkill /F /pid " + pid + "");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static List<String> read(InputStream inputStream, String charset, int portOld) {
        List<String> list = new ArrayList<String>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String port = sievePort(line, portOld);
                if (port != null && !list.contains(port)) {
                    list.add(port);
                }
            }
            bufferedReader.close();
            return list;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String sievePort(String line, int portOld) {
        if (line == null) {
            return null;
        }
        line = line.trim();
        if (line.contains("LISTENING")) {
            line = line.replaceAll("\\s+", "|");
            if (line.startsWith("TCP|")) {
                line = line.substring(4);
            }
            line = line.replace("[::]", "[]");
            int left = line.indexOf(":");
            if (left != -1) {
                left++;
                int right = line.indexOf("|", left + 1);
                String portStr = line.substring(left, right);
                if (Integer.parseInt(portStr) == portOld) {
                    int postion = line.lastIndexOf("|LISTENING|");
                    line = line.substring(postion + 11);
                    return line;
                }
            }
        } else {
            return null;
        }
        return line;
    }

    public static String readTxt(InputStream inputStream, String charset) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            bufferedReader.close();
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
